package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.dtos.SensitiveSaveDto;
import com.heima.model.wemedia.dtos.SensitiveUpdateDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.service.SensitiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-07 15:20
 */
@RestController
@RequestMapping("/api/v1/sensitive") //版本号用来处理A/B测试
@Api(tags = {"敏感词接口"},description = "敏感词接口文档")
public class SensitiveController {

    @Autowired
    private SensitiveService sensitiveService;

    /**
     * 1.list?page=1&size=10  参数可以不加任何注解，只要参数名和url上参数名一致
     * @RequestParam
     * 2./sensitive/1  /sensivie/{id} 属于url中一部分 @PathVariable("id")
     * 3.传递的是json {}  @RequestBody
     *
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("分页查询接口")
    public ResponseResult list(@RequestBody SensitiveDto dto){

        //1.校验参数
        dto.checkParam();

        //2.执行业务逻辑
        Page<WmSensitive> page = new Page<>(dto.getPage(),dto.getSize());
        page.setSize(dto.getSize());
        page.setCurrent(dto.getPage());

//        QueryWrapper<WmSensitive> wrapper = new QueryWrapper<>();
//        wrapper.like("sensitives",dto.getName()); // 冰  where sensitives like '%冰%'

        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(WmSensitive::getSensitives,dto.getName());
        }
        //链式编程简化
        Page<WmSensitive> resultPage = sensitiveService.page(page, wrapper);

//        Page<WmSensitive> resultPage = sensitiveService.page(new Page<>(dto.getPage(),dto.getSize()),
//                Wrappers.<WmSensitive>lambdaQuery().
//                like(StringUtils.isNotBlank(dto.getName()),WmSensitive::getSensitives,dto.getName()));

        //以结果为主
        PageResponseResult result = new PageResponseResult((int) resultPage.getCurrent(),
                (int) resultPage.getSize(),
                (int) resultPage.getTotal());
        result.setData(resultPage.getRecords()); //很多同学犯错 忘记加上getRecords

        return result;
    }

    @PostMapping("/save")
    public ResponseResult save(@RequestBody SensitiveSaveDto dto){
        //1.校验参数
        if(StringUtils.isBlank(dto.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            //throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.执行业务逻辑

        //2.1 查询的方式
//        WmSensitive wmSensitive = sensitiveService.getOne(Wrappers.<WmSensitive>lambdaQuery()
//                .eq(WmSensitive::getSensitives, dto.getSensitives()));

//        int count = sensitiveService.count(Wrappers.<WmSensitive>lambdaQuery()
//                .eq(WmSensitive::getSensitives, dto.getSensitives()));
//        if(count > 0)
//        if(wmSensitive != null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
//        }

        //2.2 直接保存
        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());
        wmSensitive.setCreatedTime(new Date());
        sensitiveService.save(wmSensitive);

        return ResponseResult.okResult(null);
    }


    @PostMapping("/update")
    public ResponseResult update(@RequestBody SensitiveUpdateDto dto){
        //1.校验参数
        if(StringUtils.isBlank(dto.getSensitives()) || dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            //throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.执行业务逻辑

        //2.1 查询的方式
        int count = sensitiveService.count(Wrappers.<WmSensitive>lambdaQuery()
                .eq(WmSensitive::getSensitives, dto.getSensitives()));
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //update xxx set a = '123' where id = 123
        boolean update = sensitiveService.update(Wrappers.<WmSensitive>lambdaUpdate()
                .eq(WmSensitive::getId, dto.getId())
                .set(WmSensitive::getSensitives, dto.getSensitives())
        );

        if(!update){
            //id条件不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        return ResponseResult.okResult(null);
    }

    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id){
        boolean b = sensitiveService.removeById(id);

        if(!b){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        return ResponseResult.okResult(null);
    }
}
